package com.example.kingdee.jstSync.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.KingdeeCreate.impl.CreateSalBillImpl;
import com.example.kingdee.block.JuShuiTanBlock;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.dto.PurchaseSyncDto;
import com.example.kingdee.dto.SyncDto;
import com.example.kingdee.entity.JstPurchase;
import com.example.kingdee.entity.SyncLog;
import com.example.kingdee.jstSync.JstSyncAbstract;
import com.example.kingdee.jstSync.entity.JstResult;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;
import com.example.kingdee.util.JuShuiTanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 聚水潭采购订单同步实现类
 */
@Service
public class JstPurSyncImpl extends JstSyncAbstract {

    @Autowired
    private JuShuiTanBlock juShuiTanBlock;
    @Autowired
    private CreateSalBillImpl createSalBill;
    @Autowired
    private TBxStMapper tBxStMapper;

    @Override
    public int getOrderInfoPage(SyncDto syncDto, PurchaseSyncDto purchaseSyncDtoPage) {
        return JuShuiTanUtil.getOrderInfoPage(purchaseSyncDtoPage, FormIdEnum.purchaseQuery, juShuiTanBlock.getAccessToken());
    }

    @Override
    public JstResult selectJstResult(SyncDto syncDto,  PurchaseSyncDto purchaseSyncDto) {
        JstResult jstResult = new JstResult();
        //制定查询状态
        List<String> status = new ArrayList<>();
        status.add("Confirmed");
        //先从聚水潭上查询采购单
        purchaseSyncDto.setJstFormId(FormIdEnum.purchaseQuery.name());
        purchaseSyncDto.setStatusList(status);
        String jstInfoStr = juShuiTanBlock.getJstInfo(purchaseSyncDto);
        //格式化单据
        List<JstPurchase> jstPurchases = JSONArray.parseArray(jstInfoStr, JstPurchase.class);
        jstResult.setJstPurchaseList(jstPurchases);
        return jstResult;
    }

    @Override
    public void kingdeeSync(SyncDto syncDto, JstResult jstResult) {


        List<JstPurchase> jstPurchaseList = jstResult.getJstPurchaseList();
        for (JstPurchase jstPurchase : jstPurchaseList) {
            if (!StringUtils.isEmpty(jstPurchase.getPoDate())){
                boolean before = true;
                if (!StringUtils.isEmpty(jstResult.getEndConfirmDate())){
                    //判断审核截止日期
                    Date poDate = DateUtil.parse(jstPurchase.getPoDate());
                    Date comparePoDate = DateUtil.parse(jstResult.getEndConfirmDate());
                    before = comparePoDate.before(poDate);
                }
                //判断该单号是否还有未处理的错误日志
                if (StringUtils.isEmpty(syncDto.getUserName())){
                    int isExist = tBxStMapper.isHavaErrorLog(String.valueOf(jstPurchase.getPoId()));
                    if (isExist > 0){
                        continue;
                    }
                }
                //判断单号是否存在
                int isExist = tBxStMapper.salOrderIsExist(String.valueOf(jstPurchase.getPoId()));
                if (isExist == 0 && before && jstPurchase.getStatus().equals("Confirmed")){
                    //构建数据
                    CreateInDto createInDto = new CreateInDto();
                    createInDto.setReserved1(jstPurchase.getPoId());
                    createInDto.setReserved2(jstPurchase.getPoDate());
                    createInDto.setReserved3(jstPurchase.getRemark());
                    createInDto.setReserved4(jstPurchase.getPoId());
                    createInDto.setReserved5(jstPurchase.getConfirmDate());
                    createInDto.setReserved6(jstPurchase.getWmsCoId());
                    createInDto.setReserved7(jstPurchase.getSeller());
                    createInDto.setItems(jstPurchase.getItems());
                    //创建订单
                    RequestResult result = createSalBill.createSrcBill(createInDto);
                    try {
                        if (result.getCode().equals("200")){
                            //审核订单
                            createSalBill.auditTargetBill(result.getNumber());
                        }else {
                            //删除对应单据
                            createSalBill.deleteTargetBill(result.getId());
                        }
                    }catch (Exception e){
                        //删除对应单据
                        createSalBill.deleteTargetBill(result.getId());
                    }
                    if (result.getCode().equals("200")){
                        SyncLog syncLog = new SyncLog();
                        syncLog.setJstBillno(String.valueOf(jstPurchase.getPoId()));
                        syncLog.setKingdeeBillno( result.getNumber());
                        syncLog.setObjectId(result.getId());
                        insertSyncLog(syncDto, syncLog, true);
                    }else {
                        SyncLog syncLog = new SyncLog();
                        syncLog.setJstBillno(String.valueOf(jstPurchase.getPoId()));
                        syncLog.setSyncErrorMsg(result.getMsg());
                        insertSyncLog(syncDto, syncLog, false);
                    }
                }
            }
        }
    }

    @Override
    protected void insertSyncLog(SyncDto syncDto, SyncLog syncLog, boolean isSucceed) {
        syncLog.setSyncType("采购订单同步");
        syncLog.setSrcBill("聚水潭采购单");
        syncLog.setTarBill("金蝶销售单");
        syncLog.setSyncTime(SqlTool.getNowForString());
        syncLog.setSyncUser(syncDto.getUserName() == null ? "系统自动同步" : syncDto.getUserName());
        if (isSucceed){
            syncLog.setSyncResult("同步成功");
            syncLog.setSyncErrorMsg("无");
        }else {
            syncLog.setIsDispose("未处理");
            syncLog.setSyncResult("同步失败");
        }
        tBxStMapper.insertSyncLog(syncLog);
    }
}
